package com.weibo.common;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.weibo.sql.CommonParm;

public class MailUtil {

	// 发送邮件
	public static boolean sendMail(String emailAddress, String registerurl) {
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.transport.protocol", "smtp");

		javax.mail.Session mailsession = javax.mail.Session
				.getInstance(properties);
		mailsession.setDebug(false);
		Message message = new MimeMessage(mailsession);
		Transport transport;
		try {
			message.setSubject(CommonParm.LOGIN_MAIL_TITLE);
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(
					CommonParm.LOGIN_MAIL_USERADDRESS));
			message.setContent(CommonParm.LOGIN_MAIL_CONTENT_BEFOREURL
					+ registerurl + CommonParm.LOGIN_MAIL_CONTENT_AFTERURL,
					"text/html;charset=utf-8");
			transport = mailsession.getTransport();
			transport.connect(CommonParm.LOGIN_MAIL_SMTP, 25,
					CommonParm.LOGIN_MAIL_USERNAME,
					CommonParm.LOGIN_MAIL_PASSWORD);
			transport.sendMessage(message, new Address[] { new InternetAddress(
					emailAddress) });
			transport.close();
		} catch (MessagingException e1) {
			e1.printStackTrace();
			return false;
		}
		return true;
	}
}
